﻿@model KPIS.GERP.WEB.GIMS.Controllers.AbsencQuotaFilter

@{
    ViewBag.Title = "แสดงข้อมูลโควต้าการลารายบุคคล";
    if (CompareUtility.IsObjectToStringEqual(Request["emptylayout"], "true"))
    {
        Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    }
}

@{
    string styleNonWorkingDay = "font-weight: bold; background:rgb(94, 94, 94); color:rgb(251, 251, 251);"; 
    var context = new GERPContext(true);
    var employees = context.Employees
        .Include(o => o.MasNamePrefix)
        .Include(o => o.MasEmployeeType)
        .Include(o => o.Position)
        .Include(o => o.MasEmployeeStatus)
        .Where(o => o.RecordStatus != "D" && o.MasEmployeeStatus.Active == "1")
        .OrderBy(o=>o.EmpTypeId)
        .ThenBy(o=>o.Position.PosNo);
    var absencesInCriteria = (context.PsAbsences
        .Include(o => o.AbsenceType)
        .Where(o => o.RecordStatus != "D" &&  Model.StartDate >= o.StartDate && o.StartDate <= Model.EndDate )).ToList(); 
    var absenceRules = context.RuleAbsences.Include(o=>o.AbsenceType).Where(o => o.RecordStatus != "D").ToList();
} 

<style>
    .Table1 td{
        width: 26px;
        text-align: center;
    } 
</style>

<div style="padding: 20px 0 20px 20px;">
    <h4>@ViewBag.Title</h4>
    <div style="padding: 20px 0 20px 0;">
        
        @Html.EditorFor(m => m.StartDate)
                @Html.EditorFor(m => m.EndDate)
                <button id="btnSearch">ค้นหา</button>

        <button id="btnXls">Xls</button>
    </div>
    <table border="1" class="Table1 " style="width:100%;"> 
        <tr style="font-weight: bold; background:rgb(227, 227, 227)">
            <td style="width: 180px;" rowspan="3">เจ้าหน้าที่
            </td>  
        @{
            var absenceType = context.PsAbsenceTypes.Where(o => o.RecordStatus != RecordStatus.DeletedStatus).ToList();
        } 
            <td colspan="@(absenceType.Count() *2)">
                ประเภทคำขอลา
            </td>
        </tr>
        <tr style="font-weight: bold; background:rgb(227, 227, 227)">
            @foreach(var at in absenceType)
            { 
                <td colspan="2">
                    @at.AbsenceTypeName 
                    @{
                        var absenceRule1 = absenceRules.FirstOrDefault(o => o.AbsenceTypeId == at.AbsenceTypeId);
                        if(absenceRule1 != null)
                        {
                            @:(@((absenceRule1.DayLimit ?? 0).ToString("#,##0.#")) วัน)
                        }
                    }
                </td>
            }
        </tr>
        <tr style="font-weight: bold; background:rgb(227, 227, 227)">
            @foreach(var at in absenceType)
            {  
                <td style="width: 26px;" >
                    ใช้ไป
                </td>
                <td style="width: 26px;" >
                    คงเหลือ
                </td>
            }
        </tr>
        @foreach (var e in employees)
        {
            var absenceOfEmpInCriteria = absencesInCriteria.Where(o => o.EmpId == e.EmpId); 
            <tr>
                <td style="text-align: left;">
                    <span title="@e.Position.PosNo - @e.Position.Name [@e.MasEmployeeType.Name]">
                        @e.Position.PosNo - @e.FullName 
                    </span>
                </td>
                @foreach (var at in absenceType)
                {
                    var absenceRule = absenceRules.FirstOrDefault(o => o.AbsenceTypeId == at.AbsenceTypeId);
                    decimal dayUsedInYear = 0;
                    decimal dayUsedInMonth = 0;
                    var absOfTypeInYear = absenceOfEmpInCriteria.Where(o => o.AbsenceTypeId == at.AbsenceTypeId);
                    var absOfTypeInMonth = absenceOfEmpInCriteria.Where(o => o.AbsenceTypeId == at.AbsenceTypeId);
                    foreach (var ab in absOfTypeInYear)
                    {
                        dayUsedInYear += ab.DayCount ?? 0;
                    }

                    foreach (var ab in absOfTypeInMonth)
                    {
                        dayUsedInMonth += ab.DayCount ?? 0;
                    }
                    <td>
                        @if (dayUsedInMonth == 0)
                        {
                            <span style="color: silver">@(dayUsedInMonth.ToString("#,##0.#"))</span>
                        }
                        else
                        {
                            @(dayUsedInMonth.ToString("#,##0.#"))
                        }
                       
                    </td>
                    <td> 
                        @{
                            var dayLeft = (((absenceRule == null ? 0 : absenceRule.DayLimit) - dayUsedInYear) ?? 0);
                        }
                        
                        @if (dayLeft == (absenceRule == null ? 0 : absenceRule.DayLimit))
                        {
                            <span style="color: silver">@(dayLeft.ToString("#,##0.#"))</span>
                        }
                        else
                        {
                            @(dayLeft.ToString("#,##0.#"))
                        }
                    </td>
                }
            </tr>
        }
    </table>
</div>

@section scripts {
    
    <script type="text/javascript">
        $(function () {
            $('#btnSearch').click(function() {
                var month = $('#Month').val();
                var year = $('#Year').val();
                window.location = '@Url.Action("AbsenceQuotaUsed" )?month=' + month + '&year=' + year + '&emptyLayout=@Request["EmptyLayout"]';
            });
            $('#btnXls').click(function() {
                var month = $('#Month').val();
                var year = $('#Year').val();
                window.location = '@Url.Action("AbsenceQuotaUsedExcel" )?month=' + month + '&year=' + year + '&emptyLayout=@Request["EmptyLayout"]';
            });
        });
    </script>
}